package com.pwc.conversionTool.controller;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pwc.conversionTool.XML.XMLBase;
import com.pwc.conversionTool.biz.DataAdaptorBiz;
import com.pwc.conversionTool.biz.ProjectBiz;
import com.pwc.conversionTool.biz.SoapConnectionBiz;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.Operation;
import com.pwc.conversionTool.entity.Project;
import com.pwc.conversionTool.entity.SoapConnection;

@Controller
public class ProjectController {

	private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);

	private ProjectBiz projectBiz = new ProjectBiz();

	private SoapConnectionBiz soapConnectionBiz = new SoapConnectionBiz();

	private DataAdaptorBiz dataAdaptorBiz = new DataAdaptorBiz();

	@RequestMapping(value = "/project/createProject", method = RequestMethod.GET)
	public String createProject(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		return "project/createProject";
	}

	@RequestMapping(value = "/project/viewProject", method = RequestMethod.POST)
	public String viewProject(Long id, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		try {
			Project project = projectBiz.getProjectById(id);

			model.addAttribute("project", project);
		} catch (Exception e) {

			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return "project/createProject";
	}

	@RequestMapping(value = "/project/saveProject", method = RequestMethod.POST)
	public String saveProject(Project project, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		try {
			projectBiz.createProject(project, getCurrentAccount(session));

			model.addAttribute("project", project);
		} catch (Exception e) {

			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return "project/projectDetail";
	}

	@RequestMapping(value = "/project/manageProject", method = RequestMethod.GET)
	public String manageProject(Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		try {
			List<Project> projects = projectBiz.getProjectByAccount(getCurrentAccount(session));

			model.addAttribute("projects", projects);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return "project/manageProject";
	}

	/**
	 * send in project id
	 * 
	 * @param id
	 * @param locale
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/project/operationDetail", method = RequestMethod.POST)
	public String operationDetail(Long id, Long operationId, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		
		try {
			Project project = projectBiz.getProjectById(id);
			List<SoapConnection> connections = soapConnectionBiz.getConnectionsByAccount(getCurrentAccount(session));
			List<DataTemplate> dataAdaptors = dataAdaptorBiz.getDataAdaptorsByAccount(getCurrentAccount(session));
			model.addAttribute("project", project);
			model.addAttribute("connections", connections);
			model.addAttribute("dataAdaptors", dataAdaptors);

			if (operationId != null) {
				Operation operation = projectBiz.getOperationById(operationId);
				model.addAttribute("operation", operation);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}
	

		return "project/operationDetail";
	}

	@RequestMapping(value = "/project/saveOperationDetail", method = RequestMethod.POST)
	public String saveOperationDetail(Long projectID, Long operationId, String operationName, Long soapConnectionID, Long dataAdaptorID,
			String soapTemplate, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		try {
			Operation operation = projectBiz.saveOperationDetail(projectID, operationId, soapConnectionID, dataAdaptorID, soapTemplate.trim(),
					operationName);

			boolean isXML = XMLBase.isXML(operation.getSoapTemplate());

			if (!isXML) {

				model.addAttribute("msg", "soap temp is not a xml");
				Project project = projectBiz.getProjectById(projectID);
				List<SoapConnection> connections = soapConnectionBiz.getConnectionsByAccount(getCurrentAccount(session));
				List<DataTemplate> dataAdaptors = dataAdaptorBiz.getDataAdaptorsByAccount(getCurrentAccount(session));
				model.addAttribute("project", project);
				model.addAttribute("connections", connections);
				model.addAttribute("dataAdaptors", dataAdaptors);
				model.addAttribute("operation", operation);
				return "project/operationDetail";

			}

			model.addAttribute("operation", operation);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));			
		}
		return "project/operationMapping";
	}
	
	@RequestMapping(value = "/project/viewOperationMapping", method = RequestMethod.GET)
	public String viewOperationMapping(Long operationId,Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}

		try {
			
			Operation operation=projectBiz.getOperationById(operationId);

			model.addAttribute("operation", operation);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));			
		}
		return "project/operationMapping";
	}
	
	
	

	@RequestMapping(value = "/project/viewProjectDetail", method = RequestMethod.POST)
	public String saveOperationDetail(Long id, Locale locale, Model model, HttpSession session) {
		if (!isLogin(session)) {
			return "account/login";
		}
		
		try {
			Project project = projectBiz.getProjectById(id);

			List<Operation> operations = projectBiz.getOperationsByProject(project);

			model.addAttribute("project", project);

			model.addAttribute("operations", operations);
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		

		return "project/projectDetail";
	}

}
